//
//  WBMainViewController.swift
//  微博项目
//
//  Created by 董涛 on 2017/5/26.
//  Copyright © 2017年 江汉油田. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildControllers()
       setupComposeBtn()
    }
    //使用代码控制设备方向
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
        //portrait：竖屏
        //landscape：横屏
    }
    func composeBtnClick(){
        print("涛哥点击了一下")
        
        //测试方向旋转
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.blue
        let nav = UINavigationController(rootViewController: vc)
        present(nav, animated: true, completion: nil)
    }
    fileprivate func setupComposeBtn(){
        
        // 0.添加到tabBar上
        tabBar.addSubview(composeBtn)
        
        // 1.计算按钮宽度
        let width = tabBar.bounds.width / CGFloat(viewControllers!.count)-1
        // 2.创建按钮frame
        let rect = CGRect(x: 0, y: 0, width: width, height: tabBar.bounds.height)
        // 3.设置按钮frame和偏移位
        composeBtn.frame = rect.offsetBy(dx: width * 2, dy: 0)
    }
    
    lazy var composeBtn:UIButton = {
        // 1.创建按钮
        let button = UIButton()
        // 2.设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControlState())
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        // 3.设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControlState())
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        // 4.监听加号按钮点击
        button.addTarget(self, action: #selector(WBMainViewController.composeBtnClick), for: UIControlEvents.touchUpInside)
        // 4.返回按钮
        return button
    }()
    
}
//MARK:设置所以子控制器
extension WBMainViewController{
    //设置所有子控制器
    func setupChildControllers(){
        let array = [
            ["clsName": "WBHomeViewController","title":"首页","imgeName":"home"],
            ["clsName": "WBMessageViewController","title":"消息","imgeName":"message_center"],
            ["clsName":"viwecontroller"],
            ["clsName": "WBDiscoverViewController","title":"发现","imgeName":"discover"],
            ["clsName": "WBProfileViewController","title":"我","imgeName":"profile"]
        ]
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
            viewControllers = arrayM
        }
        
    }
    //使用字典创建一个子控制器
    //parameter dicta:信息字典clsname,title,imgename
    //return:子控制器
    private func controller(dict:[String:String])->UIViewController
    {
        //1.取得字典内容
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imgeName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            else {
                return UIViewController()
        }
        //创建视图控制器
        let vc = cls.init()
        vc.title = title
        //设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_highlighted")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置tabBar标题字体（大小）
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: UIControlState(rawValue: 0))
        
        let nav = WBNavigationControllerViewController(rootViewController: vc)
        return nav
    }
}
